package com.example.recyclerview_206shj.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.recyclerview_206shj.R;
import com.example.recyclerview_206shj.bean.ArticleBean;

import java.util.List;

/**
 * Created by Raindy on 2017/4/12.
 */

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private Context context;//上下文
    private LayoutInflater layoutInflater;//动态加载布局
    private List<ArticleBean> list;//保存要显示的数据

    //自定义：构造方法，传递上下文
    public MyAdapter(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(this.context);
    }

    //自定义：传进数据源
    public void setList(List<ArticleBean> list){
        this.list = list;
    }

    //自定义：ViewHolder子类
    public class ViewHolder extends RecyclerView.ViewHolder{

        ImageView imageView;
        TextView tvtitle, tvdescript, tvtime;
        Button button;

        public ViewHolder(View itemView){
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.imageView);
            tvtitle = (TextView) itemView.findViewById(R.id.textView);
            tvdescript = (TextView) itemView.findViewById(R.id.textView2);
            tvtime = (TextView) itemView.findViewById(R.id.textView3);
            button = (Button) itemView.findViewById(R.id.button);
        }
    }

    //为了每个Item生成对应的view
    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
        return new ViewHolder(v);
    }

    //为ViewHolder中的控件填充值，设置监听
    @Override
    public void onBindViewHolder(MyAdapter.ViewHolder holder, final int position) {
        ArticleBean articleBean = list.get(position);
        if(articleBean == null){
            return;
        } else {
            //给条目中的控件设置值
            holder.imageView.setImageResource(articleBean.getImgid());
            holder.tvtitle.setText(articleBean.getTitle());
            holder.tvdescript.setText(articleBean.getDescription());
            holder.tvtime.setText(articleBean.getTime());

            //给条目中的控件设置监听器
            holder.button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    list.remove(position);
                    notifyDataSetChanged();//刷新列表
                }
            });
            //条目整体的点击事件
            holder.itemView.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    int id = list.get(position).getArticleId();
                    Toast.makeText(context, "" + id, Toast.LENGTH_SHORT).show();
                    notifyDataSetChanged();//刷新列表
                }
            });
        }

    }

    //共有多少条目
    @Override
    public int getItemCount() {
        return list.size();
    }
}
